package com.yanqu.road.server.cmd.activity.mammonhall;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.MammonHallProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import io.netty.channel.ChannelHandlerContext;

import java.util.List;

@Cmd(code = GameProtocol.S_MAMMON_HALL_REQ_USER_DATA_FROM_CROSS, desc = "财神殿-数据拉取-玩家信息")
public class MammonHallSyncUserDataCmdFromCross implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        MammonHallProto.CrossMammonHallSyncUserBaseInfoReqMsg reqMsg = MammonHallProto.CrossMammonHallSyncUserBaseInfoReqMsg.parseFrom(packet.getBytes());
        List<Long> userIdList = reqMsg.getUserIdList();


        MammonHallProto.CrossMammonHallSyncUserBaseInfoRespMsg.Builder respMsg = MammonHallProto.CrossMammonHallSyncUserBaseInfoRespMsg.newBuilder();

        for (long userId : userIdList) {
            //不在线，不更新
            GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
            if(player != null){
                MammonHallProto.CrossMammonHallSyncUserBaseInfoTemp.Builder baseBuilder = MammonHallProto.CrossMammonHallSyncUserBaseInfoTemp.newBuilder();
                baseBuilder.setUserId(player.getUserId());
                UserBaseInfo baseInfo = UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId());
                baseBuilder.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(baseInfo));
                respMsg.addInfoTemp(baseBuilder);
            }
        }

        GamePlayerMgr.sendPacket(0, YanQuMessageUtils.buildMessage(Protocol.C_MAMMON_HALL_SYNC_USER_BASE_DATA, respMsg));
    }
}
